package com.midongtech.news;

import android.content.Context;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.Log;

import androidx.annotation.NonNull;



public class DeviceUtil {



    public static String getDeviceId(Context context) {
        String id = PreferencesUtils.getInstance(context).getString("IMEI");
        if (!TextUtils.isEmpty(id)) {
            return id;
        }
        //android.telephony.TelephonyManager
        try {
            TelephonyManager mTelephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            if (mTelephony != null && mTelephony.getDeviceId() != null) {
                id = mTelephony.getDeviceId();
            } else {
                //android.provider.Settings;
                id = Settings.Secure.getString(context.getApplicationContext().getContentResolver(), Settings.Secure.ANDROID_ID);
            }
            if (TextUtils.isEmpty(id)) {
                id = getOaid(context);
                if (TextUtils.isEmpty(id)) {
                    id = getAndroidId(context);
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
            id = getOaid(context);
            if (TextUtils.isEmpty(id)) {
                id = getAndroidId(context);
            }
        }
        PreferencesUtils.getInstance(context).putString("IMEI", id);
        return id;
    }




    public static String getOaid(final Context context) {
        String oaid0 = PreferencesUtils.getInstance(context).getString("MD_OAID");
        if (TextUtils.isEmpty(oaid0) && context != null) {
            try {
                MiitHelper miitHelper = new MiitHelper(new MiitHelper.AppIdsUpdater() {
                    @Override
                    public void OnIdsAvailed(@NonNull String oaid, boolean isSupport) {
                        Log.e("hyw", "ids:" + oaid);
                        if (isSupport && !TextUtils.isEmpty(oaid)) {
                            PreferencesUtils preferencesUtils = PreferencesUtils.getInstance(context);
                            preferencesUtils.putString("MD_OAID", oaid);
                        }
                    }
                });
                miitHelper.getDeviceIds(context);
                miitHelper.getDeviceIds(context);
            } catch (Exception e) {
                e.printStackTrace();
                Log.e("hyw", "getOaid Exception:" + e.getMessage());
            }
        }
        return oaid0;
    }





    /**
     * 获得设备的AndroidId
     *
     * @param context 上下文
     * @return 设备的AndroidId
     */
    public static String getAndroidId(Context context) {
        String androidId = PreferencesUtils.getInstance(context).getString("ANDROID_ID");
        if (TextUtils.isEmpty(androidId)) {
            try {
                androidId = Settings.Secure.getString(context.getContentResolver(),
                        Settings.Secure.ANDROID_ID);

                PreferencesUtils.getInstance(context).putString("ANDROID_ID", androidId);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        return androidId;
    }


}
